<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input v-model="ruleForm.pass" type="password"></el-input>
      </el-form-item>
      <el-form-item class="button">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "HomeView",
  data() {
    // 验证器
    // 验证用户名
    var validateName = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入用户名"));
      } else if (/^[^\\/:*?"'<>|]*$/.test(value) == false) {
        callback(new Error("包含非法字符，请重新输入"));
      } else {
        callback();
      }
    };
    // 验证密码
    var validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码至少6位"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        pass: "",
      },
      rules: {
        name: [{ validator: validateName, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("登陆成功!");
          this.$router.push("/about");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss">
.login {
  height: 100%;
  background-image: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.pptbz.com%2Fd%2Ffile%2Fp%2F201708%2Ffc3f0a648259e7cc2c14d5271294d6c0.jpg&refer=http%3A%2F%2Fwww.pptbz.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665210941&t=bb9dfeb69091710c39966cc944bd2fda");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    width: 500px;
    height: 300px;
    background-color: white;
    border-radius: 20px;
    padding: 50px 0;
    box-sizing: border-box;
    opacity: 0.9;
    .button {
      margin-left: 70px;
      .el-form-item__content {
        line-height: 50px;
      }
    }
    input {
      width: 340px;
    }
  }
}
</style>
